<?php


/**
 * initialize frontend
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @version 1
 * @package core
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * initialize the frontend
 *
 * @package core
 * @version 1
 */
class frontend
{
	/**
	 * @var object instance
	 */
	private static $INSTANCE;
	/**
	 * @var object Database class
	 */
	private $DB;
	/**
	 * @var object i18n
	 */
	private $I18N;
	/**
	 * @var object configuration
	 */
	private $CONFIG;
	/**
     * @var object user configuration
     */
    private $USERCONFIG;
	/**
	 * @var object session class
	 */
	private $SESSION = false;
	/**
	* @var string 	name of old errorhandler
	*/
	public $OLDERRORHANDLER = null;
	/**
	* @var string bool true when new error handler is set
	*/
	public $NEWERRORHANDLER = null;
    /**
    * @var string   name of old exception handler
    */
    public $OLDEXCEPTIONHANDLER = null;
    /**
    * @var string   name of new exception handler function
    */
    public $NEWEXCEPTIONHANDLER = null;
	/**
	* @var string bool true when new handler is set
	*/
	private $PHPFailureText = null;
	/**
	* @var	array		volatile memory
	*/
	private $MERKER = array ();
	/**
	* set a named volatile memory
	* @param 	string	vol. memory name
	* @param 	mixed	vol. memory value
	* @return	void
	*/
	public function SetMEM($name = null, $val)
	{
		$this->MERKER[$name] = $val;
	}
	/**
	 * set a named volatile memory
	 * @param 	string	vol. memory name
	 * @return   mixed	value of mem.
	 */
	public function GetMEM($name = null)
	{
		return $this->MERKER[$name];
	}
	/**
	 * instantiate essential objects and make them avail. as subobjects
	 * @param string session name (optional)
	 * @return	void
	 */
	private function __construct($sessionname = 'PHPSESSION')
	{
		$this->CONFIG = new config();
		$this->DB = database::getInstance();;
		if (!is_object($this->SESSION))
			$this->SetSession(session::getInstance($sessionname));
        $this->InitUserconfig ();
		$this->setErrorHandler();
		$this->setExceptionHandler();
	}
	public function restoreErrorHandler ()
	{
	    if ($this->OLDERRORHANDLER && $this->NEWERRORHANDLER)
	    {
	        set_error_handler($this->OLDERRORHANDLER);
	        $this->NEWERRORHANDLER = false;
	    }
	}
    public function setExceptionHandler ()
    {
        if ($this->NEWEXCEPTIONHANDLER)
           return;
        $this->OLDEXCEPTIONHANDLER = set_exception_handler('myExceptionHandler');
        $this->NEWEXCEPTIONHANDLER = true;
    }
	public function setErrorHandler ()
	{
	    if ($this->NEWERRORHANDLER)
	       return;
	    $this->OLDERRORHANDLER = set_error_handler('myErrorHandler');
        $this->NEWERRORHANDLER = true;
	}
    /**
     * Init userconfig if session is avail. and user is logged in
     *
     * @return  void
     */
    public function InitUserconfig ($int_userid = 0)
    {
        if (!$int_userid)
        {
            $int_userid = $this->SESSION->user_id ? $this->SESSION->user_id : '0';
        }
        $this->USERCONFIG = new userconfig($int_userid);
    }
	/**
	 * set a session object
	 *
	 * @param 	object	session object
	 * @return	void
	 */
	public function SetSession ($session)
	{
		if (!is_object($this->SESSION))
			$this->SESSION = session::getInstance();
		return;
	}
	/**
	 * return a instance of object
	 *
	 * @param string session name (optional)
	 * @return object instance
	 */
	public static function getInstance($sessionname = "PHPSESSION")
	{
        if (!isset(self::$INSTANCE)) {
            $c = __CLASS__;
            self::$INSTANCE = new $c ($sessionname);
        }
        return self::$INSTANCE;
    }
	/**
	 * set i18 object
	 *
	 * @param object instance of i18n
	 * @return void
	 */
	public function setI18n($object = null)
	{
    	$this->I18N = $object;
    	return;
    }
    /**
	 * get i18 object
	 *
	 * @return object
	 */
	public function getI18n()
	{
    	return $this->I18N;
    }
    /**
	 * get config object
	 *
	 * @return object
	 */
	public function getConfig()
	{
    	return $this->CONFIG;
    }
    /**
     * get user config object
     *
     * @return object
     */
    public function getUserConfig()
    {
        return $this->USERCONFIG;
    }
    /**
	 * get DB object
	 *
	 * @return object
	 */
	public function getDB()
	{
    	return $this->DB;
    }
    /**
     * get session object
     *
     * @return object
     */
    public function getSession()
    {
        return $this->SESSION;
    }
}